namespace Admin.Domain.Entities;

public class Role : EntityBase
{
    public Role()
    {
        
    }
    /// <summary>
    /// 角色名称
    /// </summary>
    public string RoleName { get; set; } = null!;

    /// <summary>
    /// 权限集合
    /// </summary>
    public virtual ICollection<Permission> Permissions { get; set; } = new HashSet<Permission>();

    /// <summary>
    /// 用户集合
    /// </summary>
    public virtual ICollection<User> Users { get; set; } = new HashSet<User>();

    /// <summary>
    /// 私有的构造函数
    /// </summary>
    private Role(string roleName)
    {
        RoleName = roleName;
    }

    /// <summary>
    /// 注册新角色
    /// </summary>
    /// <param name="roleName">角色名</param>
    /// <returns>角色对象 或 null</returns>
    public static Role? CreateRole(string roleName)
    {
        if (!ValidateRoleName(roleName))
        {
            return null;
        }

        return new Role(roleName);
    }

    /// <summary>
    /// 软删除角色
    /// </summary>
    /// <returns>是否删除成功</returns>
    public bool DeleteRole()
    {
        if (IsDeleted)
        {
            return false;
        }
        IsDeleted = true;
        return true;
    }

    /// <summary>
    /// 更新角色信息
    /// </summary>
    /// <param name="newRoleName">新的角色名</param>
    /// <param name="newDescription">新的简介</param>
    /// <returns>是否修改成功</returns>
    public bool UpdateUserInfo(string newRoleName, string? newDescription)
    {
        // 验证新的角色名是否有效
        if (!ValidateRoleName(newRoleName))
        {
            return false;
        }

        // 检查用户是否已经被软删除
        if (IsDeleted)
        {
            return false;
        }

        // 更新角色名
        RoleName = newRoleName;
        Description = newDescription;
        // 返回更新成功
        return true;
    }

    /// <summary>
    /// 赋予权限
    /// </summary>
    /// <param name="permission">赋予的权限</param>
    /// <returns>是否赋予成功</returns>
    public bool AssignPermission(Permission permission)
    {
        // 检查角色是否已经被软删除
        if (IsDeleted)
        {
            return false;
        }

        Permissions.Add(permission);
        return true;
    }

    /// <summary>
    /// 移除权限
    /// </summary>
    /// <param name="permission">移除的权限</param>
    /// <returns>是否移除成功</returns>
    public bool RemovePermission(Permission permission)
    {
        // 检查角色是否已经被软删除
        if (IsDeleted)
        {
            return false;
        }

        Permissions.Remove(permission);
        return true;
    }

    /// <summary>
    /// 添加被赋予角色的用户
    /// </summary>
    /// <param name="user">被赋予角色的用户</param>
    /// <returns>是否添加成功</returns>
    public bool AddUser(User user)
    {
        // 检查角色是否已经被软删除
        if (IsDeleted)
        {
            return false;
        }

        Users.Add(user);
        return true;
    }

    /// <summary>
    /// 移除被赋予角色的用户
    /// </summary>
    /// <param name="user">被移除角色的用户</param>
    /// <returns>是否移除成功</returns>
    public bool RemoveUser(User user)
    {
        // 检查角色是否已经被软删除
        if (IsDeleted)
        {
            return false;
        }

        Users.Remove(user);
        return true;
    }

    /// <summary>
    /// 启用角色
    /// </summary>
    /// <returns>是否启用成功</returns>
    public bool EnableRole()
    {
        // 检查角色是否已经被软删除
        if (IsDeleted)
        {
            return false;
        }

        // 检查角色是否已经被启用
        if (IsActive)
        {
            return false;
        }

        IsActive = true;

        // 返回成功
        return true;
    }

    /// <summary>
    /// 禁用角色
    /// </summary>
    /// <returns>是否禁用成功</returns>
    public bool DisableRole()
    {
        // 检查角色是否已经被软删除
        if (IsDeleted)
        {
            return false;
        }

        // 检查角色是否已经被禁用
        if (!IsActive)
        {
            return false;
        }

        IsActive = false;

        // 返回成功
        return true;
    }

    /// <summary>
    /// 验证角色名
    /// </summary>
    /// <param name="roleName">角色名</param>
    private static bool ValidateRoleName(string roleName)
    {
        // 验证角色名是否为空
        if (string.IsNullOrWhiteSpace(roleName))
        {
            return false;
        }

        // 可以添加其他验证

        return true;
    }
}